package com.example.coinhelper.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.coinhelper.Bean.Details;
import com.example.coinhelper.R;
import com.example.coinhelper.utils.TimeUtility;

import java.text.DecimalFormat;
import java.time.YearMonth;
import java.util.List;

public class TopLabelsAdapter extends BaseAdapter {
    private List<Details> detailsList;
    private Context context;
    public TopLabelsAdapter(Context context,List<Details> detailsList){
        this.context = context;
        this.detailsList = detailsList;
    }
    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView =  LayoutInflater.from(context).inflate(R.layout.item_details, parent, false);
        TextView topOutTv, topInTv, topbudgetTv, topConTv;
        double sum_in = 0, sum_out = 0, todaySumIn = 0, todaySumOut = 0;

        for (Details details : detailsList) {
            System.out.println("零钱："+details.getDate());
            if (TimeUtility.isInCurrentMonth(details.getDate())) {
                System.out.println("零钱："+details.getMoney());
                if (details.getMoney() < 0) {
                    sum_out += details.getMoney();
                } else {
                    sum_in += details.getMoney();
                }
            }
            if (TimeUtility.isToday(details.getDate())) {
                System.out.println("零钱："+details.getMoney());
                if (details.getMoney() < 0) {
                    todaySumOut += details.getMoney();
                } else {
                    todaySumIn += details.getMoney();
                }
            }
        }
        YearMonth yearMonth = YearMonth.now();
        int daysInMonth = yearMonth.lengthOfMonth();
        double avgDailyExpenditure = sum_out / daysInMonth;

        DecimalFormat decimalFormat = new DecimalFormat("#.##");
        String formattedSumIn = decimalFormat.format(sum_in);
        String formattedSumOut = decimalFormat.format(sum_out);
        String formattedAvgDailyExpenditure = decimalFormat.format(avgDailyExpenditure);
        String formattedTodaySumIn = decimalFormat.format(todaySumIn);
        String formattedTodaySumOut = decimalFormat.format(todaySumOut);
        //System.out.println(detailsList.size());
        topOutTv = convertView.findViewById(R.id.item_mainlv_top_tv_out);
        topInTv = convertView.findViewById(R.id.item_main_top_tv_in);
        topbudgetTv = convertView.findViewById(R.id.item_mainlv_top_tv_budget);
        topConTv = convertView.findViewById(R.id.item_mainlv_top_tv_day);
        topOutTv.setText("￥" + formattedSumOut);
        topInTv.setText("￥" + formattedSumIn);
        topbudgetTv.setText("￥" + formattedAvgDailyExpenditure);
        topConTv.setText("今日支出￥" + formattedTodaySumOut + " 今日收入￥" + formattedTodaySumIn);
        return convertView;
        //System.out.println("￥" + formattedSumOut+"\n￥" + formattedSumIn+"\n￥" + formattedAvgDailyExpenditure+"\n今日支出￥" + formattedTodaySumOut + " 今日收入￥" + formattedTodaySumIn);
    }
}
